﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LEVUtilityLibrary;
using B2BToolKitControl;
using LEVBatteryPackControl;
using LEV_ST_OneWireDataResolution;

namespace LEV_Kits_Data_Transform_Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            B2BCmdListUpdated();
            LEVCmdListUpdated();
            OneWireCmdListUpdated();
            
        }


        #region B2B Example codes ==> using B2BToolKitControl;
        void B2BCmdListUpdated()
        {
            string[] name = Enum.GetNames(typeof(B2BCommandProvider.Cmd_Calibration));
            B2B_Cal_comboBox.Items.AddRange(name);
            name = Enum.GetNames(typeof(B2BCommandProvider.Cmd_Control));
            B2B_Control_comboBox.Items.AddRange(name);
            name = Enum.GetNames(typeof(B2BCommandProvider.Cmd_DataTransmit));
            B2B_DataTrans_comboBox.Items.AddRange(name);
        }
        private void B2B_Cal_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            B2B_Cmd_Name_textBox.Text = B2B_Cal_comboBox.Items[B2B_Cal_comboBox.SelectedIndex].ToString();
            B2BCommandProvider.Cmd_Calibration calValue = (B2BCommandProvider.Cmd_Calibration)Enum.Parse(typeof(B2BCommandProvider.Cmd_Calibration), B2B_Cmd_Name_textBox.Text);
            B2B_Cmd_textBox.Text = String.Format("0x{0:X}",(byte)calValue);
        }
        private void B2B_Control_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            B2B_Cmd_Name_textBox.Text = B2B_Control_comboBox.Items[B2B_Control_comboBox.SelectedIndex].ToString();
            B2BCommandProvider.Cmd_Control calValue = (B2BCommandProvider.Cmd_Control)Enum.Parse(typeof(B2BCommandProvider.Cmd_Control), B2B_Cmd_Name_textBox.Text);
            B2B_Cmd_textBox.Text = String.Format("0x{0:X}", (byte)calValue);
        }

        private void B2B_DataTrans_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            B2B_Cmd_Name_textBox.Text = B2B_DataTrans_comboBox.Items[B2B_DataTrans_comboBox.SelectedIndex].ToString();
            B2BCommandProvider.Cmd_DataTransmit calValue = (B2BCommandProvider.Cmd_DataTransmit)Enum.Parse(typeof(B2BCommandProvider.Cmd_DataTransmit), B2B_Cmd_Name_textBox.Text);
            B2B_Cmd_textBox.Text = String.Format("0x{0:X}", (byte)calValue);
        }
        private void TransB2BKitCmd_button_Click(object sender, EventArgs e)
        {
            byte[] packet = B2BCommandProvider.CmdToB2BToolKit(Convert.ToByte(B2B_Cmd_textBox.Text, 16), MassUtilities.HexStringToByteArray(Parameter_textBox.Text));
            B2B_Packet_textBox.Text = MassUtilities.ByteArrayToHexString(packet, true);
        }
        private void B2B_Packet_Unpack_button_Click(object sender, EventArgs e)
        {
            B2BReceivedDataDetector receiver = new B2BReceivedDataDetector();
            List<B2BReceivedDataDetector.ReceivedPacket> packet = receiver.DataDetector(MassUtilities.HexStringToByteArray(B2B_Packet_textBox.Text));
            Unpack_textBox.Text = ("=============================" + Environment.NewLine);
            foreach (B2BReceivedDataDetector.ReceivedPacket p in packet)
            {
                Unpack_textBox.AppendText("RX Cmd : " + String.Format("0x{0:X}", p.Cmd) + Environment.NewLine);
                string data = MassUtilities.ByteArrayToHexString(p.Data, true);
                Unpack_textBox.AppendText("RX data : " + data + Environment.NewLine);
                Unpack_textBox.AppendText("RX data type : " + p.Type + Environment.NewLine);
                Unpack_textBox.AppendText("=============================" + Environment.NewLine);
            }
            //
        }
        private void B2B_Cmd_textBox_KeyDown(object sender, KeyEventArgs e)
        {
            B2B_Cmd_Name_textBox.Text = "User Input";
        }
        #endregion

        #region LEV Battery Example codes ==> using LEVBatteryPackControl;
        void LEVCmdListUpdated()
        {
            string[] name = Enum.GetNames(typeof(LEVBPCommandProvider.Cmd_GetInform));
            this.LEV_Get_Cmd_comboBox.Items.AddRange(name);
            name = Enum.GetNames(typeof(LEVBPCommandProvider.Cmd_SetInform));
            this.LEV_Set_Cmd_comboBox.Items.AddRange(name);
        }


        private void LEV_Get_Cmd_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LEV_Cmd_Name_textBox.Text = LEV_Get_Cmd_comboBox.Items[LEV_Get_Cmd_comboBox.SelectedIndex].ToString();
            LEVBPCommandProvider.Cmd_GetInform value = (LEVBPCommandProvider.Cmd_GetInform)Enum.Parse(typeof(LEVBPCommandProvider.Cmd_GetInform), LEV_Cmd_Name_textBox.Text);
            LEV_Cmd_textBox.Text = String.Format("0x{0:X}", (byte)value);
        }

        private void LEV_Set_Cmd_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LEV_Cmd_Name_textBox.Text = LEV_Set_Cmd_comboBox.Items[LEV_Set_Cmd_comboBox.SelectedIndex].ToString();
            LEVBPCommandProvider.Cmd_SetInform value = (LEVBPCommandProvider.Cmd_SetInform)Enum.Parse(typeof(LEVBPCommandProvider.Cmd_SetInform), LEV_Cmd_Name_textBox.Text);
            LEV_Cmd_textBox.Text = String.Format("0x{0:X}", (byte)value);
        }

        private void Trans_LEVBPPacket_button_Click(object sender, EventArgs e)
        {
            byte[] packet = LEVBPCommandProvider.CmdToLEVBP(Convert.ToByte(LEV_Cmd_textBox.Text, 16), MassUtilities.HexStringToByteArray(LEV_Parameter_textBox.Text));
            LEVBP_Packet_textBox.Text = MassUtilities.ByteArrayToHexString(packet, true);
        }

        private void TransLEVBPviaB2BKit_button_Click(object sender, EventArgs e)
        {
            byte[] packet = B2BCommandProvider.CmdToLevBatteryViaB2BToolKit(Convert.ToByte(LEV_Cmd_textBox.Text, 16), MassUtilities.HexStringToByteArray(LEV_Parameter_textBox.Text));
            LEV_Packet_via_B2B_textBox.Text = MassUtilities.ByteArrayToHexString(packet, true);

        }

        private void LEVBP_Packet_Unpack_button_Click(object sender, EventArgs e)
        {
            LEVBPReceivedDataDetector receiver = new LEVBPReceivedDataDetector();
            List<LEVBPReceivedDataDetector.ReceivedPacket> packet = receiver.DataDetector(MassUtilities.HexStringToByteArray(LEVBP_Packet_textBox.Text));


            LEV_Unpacking_textBox.Text = ("=============================" + Environment.NewLine);
            foreach (LEVBPReceivedDataDetector.ReceivedPacket p in packet)
            {
                LEV_Unpacking_textBox.AppendText("RX Cmd : " + String.Format("0x{0:X}", p.Cmd) + Environment.NewLine);
                string data = MassUtilities.ByteArrayToHexString(p.Data, true);
                LEV_Unpacking_textBox.AppendText("RX data : " + data + Environment.NewLine);
                LEV_Unpacking_textBox.AppendText("RX data type : " + p.Type + Environment.NewLine);
                LEV_Unpacking_textBox.AppendText("=============================" + Environment.NewLine);
            }

        }

        private void LEV_Cmd_textBox_KeyDown(object sender, KeyEventArgs e)
        {
            LEV_Cmd_Name_textBox.Text = "User Input";
        }
        #endregion

        #region LEV One-Wire Data Resolution Example codes ==> using LEV_ST_OneWireDataResolution;
        LEV_ST_G2_OneWireDataResolution g_Owdr = new LEV_ST_G2_OneWireDataResolution();
        private void OneWire_systemData_Set_button_Click(object sender, EventArgs e)
        {
            //detection packet
            string str = MassUtilities.StringTrim_Replace_EnterAndTab_CombineMultiSpaceToOne(OneWire_SystemData_textBox.Text);
            LEVBPReceivedDataDetector receiver = new LEVBPReceivedDataDetector();
            List<LEVBPReceivedDataDetector.ReceivedPacket> packets = receiver.DataDetector(MassUtilities.HexStringToByteArray(str));
            bool find_data_flag = false;
            byte[] find_data = new byte[0] ;
            foreach (LEVBPReceivedDataDetector.ReceivedPacket packet in packets)
            {
                if (packet.Type == LEVBPReceivedDataDetector.RespondedDataType.LEV_ST_G2_OneWire_SystemData)
                {
                    find_data = packet.Data;
                    find_data_flag = true;
                    break;
                }
            }
            if (!find_data_flag)
            {
                OneWire_System_infor_textBox.Text = ("Cannot find System Status Data packet (structure)." + Environment.NewLine);
                return;
            }
            
            g_Owdr.SetData(LEV_ST_G2_OneWireDataResolution.DataGroup.SystemStatus, find_data);

            LEV_ST_G2_OneWireDataResolution.DetailOfItem[] items;
            items = g_Owdr.GetAllItemInfor(typeof(LEV_ST_G2_OneWireDataResolution.ItemNameOfSystemStatus));

            OneWire_System_infor_textBox.Text = ("==============================================" + Environment.NewLine);
            foreach (LEV_ST_G2_OneWireDataResolution.DetailOfItem item in items)
            {
                OneWire_System_infor_textBox.AppendText(item.Name.PadRight(48) + "= " + item.Values.ToString().PadRight(10) + "(type = " + item.Type + ")" + Environment.NewLine);
            }
            OneWire_System_infor_textBox.AppendText("==============================================" + Environment.NewLine);
        }

        void OneWireCmdListUpdated()
        {
            string[] name = Enum.GetNames(typeof(LEV_ST_G2_OneWireDataResolution.ItemNameOfSystemStatus));
            OneWireSystemValuesSelect_comboBox.Items.AddRange(name);
            name = Enum.GetNames(typeof(LEV_ST_G2_OneWireDataResolution.ItemNameOfEepromData));
            OneWireEEpromValuesSelect_comboBox.Items.AddRange(name);

            name = Enum.GetNames(typeof(LEV_ST_G2_OneWireDataResolution.BitFlagItemsOfSystem));
            BitFlagOfSystem_comboBox.Items.AddRange(name);
        }
        private void OneWireSystemValuesSelect_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = OneWireSystemValuesSelect_comboBox.Items[OneWireSystemValuesSelect_comboBox.SelectedIndex].ToString();
            //OneWireDataResolution.DetailOfItem item = g_Owdr.GetItemInfor(typeof(OneWireDataResolution.ItemNameOfSystemStatus), str);
            LEV_ST_G2_OneWireDataResolution.DetailOfItem item = g_Owdr.GetItemInfor < LEV_ST_G2_OneWireDataResolution.ItemNameOfSystemStatus>(str);
            OneWireSystemValues_textBox.Text = (item.Name.PadRight(48) + "= " + item.Values.ToString().PadRight(10) + "(type = " + item.Type + ")" + Environment.NewLine);
        }

        private void OneWire_eepromData_Set_button_Click(object sender, EventArgs e)
        {
            //detection packet
            string str = MassUtilities.StringTrim_Replace_EnterAndTab_CombineMultiSpaceToOne(OneWire_EEpromData_textBox.Text);
            LEVBPReceivedDataDetector receiver = new LEVBPReceivedDataDetector();
            List<LEVBPReceivedDataDetector.ReceivedPacket> packets = receiver.DataDetector(MassUtilities.HexStringToByteArray(str));
            bool find_data_flag = false;
            byte[] find_data = new byte[0];
            foreach (LEVBPReceivedDataDetector.ReceivedPacket packet in packets)
            {
                if (packet.Type == LEVBPReceivedDataDetector.RespondedDataType.LEV_ST_G2_OneWire_EEPROM)
                {
                    find_data = packet.Data;
                    find_data_flag = true;
                    break;
                }
            }
            if (!find_data_flag)
            {
                OneWire_eeprom_infor_textBox.Text = ("Cannot find EEprom Data packet (structure)." + Environment.NewLine);
                return;
            }

            g_Owdr.SetData(LEV_ST_G2_OneWireDataResolution.DataGroup.EepromData, find_data);

            LEV_ST_G2_OneWireDataResolution.DetailOfItem[] items;
            items = g_Owdr.GetAllItemInfor(typeof(LEV_ST_G2_OneWireDataResolution.ItemNameOfEepromData));

            OneWire_eeprom_infor_textBox.Text = ("==============================================" + Environment.NewLine);
            foreach (LEV_ST_G2_OneWireDataResolution.DetailOfItem item in items)
            {
                OneWire_eeprom_infor_textBox.AppendText(item.Name.PadRight(58) + "= " + item.Values.ToString().PadRight(10) + "(type = " + item.Type + ")" + Environment.NewLine);
            }
            OneWire_eeprom_infor_textBox.AppendText("==============================================" + Environment.NewLine);

        }

        private void OneWireEEpromValuesSelect_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = OneWireEEpromValuesSelect_comboBox.Items[OneWireEEpromValuesSelect_comboBox.SelectedIndex].ToString();
            LEV_ST_G2_OneWireDataResolution.DetailOfItem item = g_Owdr.GetItemInfor(typeof(LEV_ST_G2_OneWireDataResolution.ItemNameOfEepromData), str);
            if (LEV_ST_G2_OneWireDataResolution.ItemNameOfEepromData.MANUFACTURE_DATE_Var_uint_offset_106.ToString().Contains(item.Name))
            {
                OneWireEEpromValues_textBox.Text = (item.Name.PadRight(58) + "= " + item.Values.ToString().PadRight(10) + "(type = " + item.Type + ")(" +
                    MassUtilities.LEV_TransformLevDate((short)item.Values) + ")" + Environment.NewLine);
            }
            else
            {
                OneWireEEpromValues_textBox.Text = (item.Name.PadRight(58) + "= " + item.Values.ToString().PadRight(10) + "(type = " + item.Type + ")" + Environment.NewLine);
            }
        }

        private void BitFlagOfSystem_comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            string str = BitFlagOfSystem_comboBox.Items[BitFlagOfSystem_comboBox.SelectedIndex].ToString();
            //OneWireDataResolution.DetailOfItem item = g_Owdr.GetItemInfor(typeof(OneWireDataResolution.ItemNameOfSystemStatus), str);
            LEV_ST_G2_OneWireDataResolution.BitFlagItemsOfSystem selectItem = (LEV_ST_G2_OneWireDataResolution.BitFlagItemsOfSystem)Enum.Parse(typeof(LEV_ST_G2_OneWireDataResolution.BitFlagItemsOfSystem), str);
            bool itemFlag = g_Owdr.GetBitFlagOfSystem(selectItem);
            LEV_ST_G2_OneWireDataResolution.DetailBitFlagOfItemOfSystem item = g_Owdr.GetDetailBitFlagOfSystem(selectItem);
            BitFlagOfsystem_textBox.Text = (item.Name + " = " + item.flag + Environment.NewLine);

        }
        #endregion
    }
}
